Redis Explained

Redis Explained

A simple introduction to Redis and how it speeds up modern applications.

Modern backend systems need to be fast, scalable, and efficient. Users expect applications to respond instantly, but as applications grow, performance often becomes a challenge.

In a typical web application, when a user requests data, the server queries a database like PostgreSQL or MongoDB. This works well initially, but imagine an API receiving thousands of requests every second. If every request hits the database, complex queries and disk access can quickly slow things down.

Over time, the database becomes a performance bottleneck, increasing response times and degrading user experience.

This is where Redis comes in.

Redis is an in-memory data store that allows applications to retrieve data extremely fast by storing frequently accessed data in memory. Because of this, it has become one of the most widely used technologies in modern backend systems. Companies like Twitter, GitHub, Pinterest, and StackOverflow use Redis to improve performance and handle large-scale traffic.


What is Redis?

Redis stands for Remote Dictionary Server.

It is an in-memory key–value data store used as a database, cache, and message broker.

Unlike traditional databases that store data on disk, Redis stores data in memory (RAM). Because of this, Redis can perform operations extremely fast often in microseconds.

At its core, Redis works like a dictionary:

 
key → value
 

Example:

 
user:1 → "john"
product:12 → "Laptop"
session:abc123 → user data
 

Why is Redis So Fast?

The main reason Redis is fast is because it stores data in RAM instead of disk.

Traditional databases need to:

  • read from disk
  • parse queries
  • access storage layers

Redis skips most of this, making the workflow much simpler:

 
Application → Redis (RAM) → Response
 

Instead of milliseconds, Redis responses often take microseconds.

This makes Redis ideal for situations where speed is critical.


Redis Data Structures

Redis supports multiple powerful data structures.

1. Strings

The simplest and most common data type.

Example:

SET name "Saurabh"
GET name

2. Lists

Lists store ordered collections of elements, similar to an array.

Example:

LPUSH tasks "task1"
LPUSH tasks "task2"

3. Sets

Sets store unique values.

Example:

SADD users "user1"
SADD users "user2"

4. Hashes

Hashes store objects with fields and values.

Example:

HSET user:1 name "Saurabh"
HSET user:1 age 18

5. Sorted Sets

Sorted sets store values with a score, allowing ordered data.

Example:

ZADD leaderboard 100 "player1"
ZADD leaderboard 200 "player2"

Real World Use Cases of Redis

Redis is rarely used as a primary database. Instead, it is commonly used as a performance layer.

Here are some real-world use cases.

1. Caching

Caching is the most common Redis use case.

Example workflow:

Client → API → Redis → Database

Process:

  1. API checks Redis first
  2. If data exists → return instantly
  3. If not → fetch from database
  4. Store result in Redis

2. Rate Limiting

APIs often limit how many requests a user can make.

Example:

User can only make 100 requests per minute

Redis can track request counts efficiently using commands like:

INCR
EXPIRE

This is commonly used in public APIs.


3. Job Queues

Redis lists can be used to create task queues.

Example tasks:

  • sending emails
  • processing images
  • background notifications

Workers pull tasks from Redis and process them.


4. Leaderboards

Gaming platforms often use Redis sorted sets.

Example:

player → score

Redis can instantly return:

  • top players
  • rankings
  • score updates

A Simple Way to Think About Redis

You can think of Redis as a super-fast layer that sits between your application and the database.

Instead of asking the database for the same data again and again, the application can quickly fetch it from Redis, reducing load on the database and improving response times.


Resources

Designed & Developed by Saurabh.